<script lang="ts" setup>
import { menusRoutes } from '@/router'
import { useSettingsStore } from '@/stores/modules/settings'

interface MenuProps {
  mode: 'vertical' | 'horizontal'
}

const props = withDefaults(defineProps<MenuProps>(), {
  mode: 'vertical'
})

const { menuCollapse, systemTitle } = toRefs(useSettingsStore())
</script>

<template>
  <div :class="props.mode === 'vertical' ? 'layout-menu-vertical' : 'layout-menu-horizontal'">
    <div class="system-title" :class="{ collapsed: menuCollapse }" @click="$router.push('/')">
      <zc-svg-icon name="logo" size="2em" />
      <div class="title" v-show="!menuCollapse">{{ systemTitle }}</div>
    </div>

    <el-menu
      unique-opened
      collapse-transition
      class="el-menu-vertical-demo"
      :collapse="menuCollapse"
      router
      :default-active="$route.path"
      :mode="props.mode"
    >
      <template v-for="menu in menusRoutes" :key="menu.path">
        <!-- 多层级 -->
        <el-sub-menu :index="menu.path" v-if="menu.children && menu.children.length > 0">
          <template #title>
            <zc-svg-icon :name="(menu.meta?.icons as string) || 'default'" />
            <span>{{ menu.meta?.title }}</span>
          </template>
          <el-menu-item
            v-for="childMenu in menu.children"
            :key="childMenu.path"
            :index="childMenu.path"
          >
            <zc-svg-icon :name="(childMenu.meta?.icons as string) || 'default'" />
            <span>{{ childMenu.meta?.title }}</span>
          </el-menu-item>
        </el-sub-menu>

        <!-- 单层级 -->
        <!-- <el-menu-item v-else :index="menu.path">
          <zc-svg-icon :name="(menu.meta?.icons as string) || 'default'" />
          <span>{{ menu.meta?.title }}</span>
        </el-menu-item> -->
      </template>
    </el-menu>
  </div>
</template>

<style scoped lang="scss">
.layout-menu-vertical {
  height: 100%;
  box-sizing: border-box;

  .system-title {
    height: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 5px;
    max-width: 200px;
    overflow: hidden;
    transition: all 0.3s;
    cursor: pointer;

    &.collapsed {
      padding: 0 10px;
    }

    .title {
      font-size: 22px;
      font-weight: bold;
      white-space: nowrap;
      transition: opacity 0.3s;
    }
  }

  .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 600px;
  }

  .el-menu {
    height: calc(100% - 60px);
    border: none;
    overflow-y: auto;
    overflow-x: hidden;

    :deep(.el-sub-menu__title) {
      gap: 8px;
    }

    :deep(.el-menu-item) {
      gap: 8px;
    }

    .el-menu-item.is-active {
      background-color: var(--el-fill-color-darker);
      color: var(--el-color-primary);
      font-weight: bold;
    }
  }
}

.layout-menu-horizontal {
  display: flex;
  align-items: center;
  height: var(--zc-nav-height);

  .system-title {
    height: 100%;
    width: 200px;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 5px;
    max-width: 200px;
    overflow: hidden;
    transition: all 0.3s;

    &.collapsed {
      padding: 0 10px;
    }

    .title {
      font-size: 22px;
      font-weight: bold;
      white-space: nowrap;
      transition: opacity 0.3s;
    }
  }

  .el-menu {
    flex: 1;
    border: none;
  }
}
</style>
